package com.zrrd.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zrrd.model.common.dtos.PageResponseResult;
import com.zrrd.model.common.dtos.ResponseResult;
import com.zrrd.model.common.enums.AppHttpCodeEnum;
import com.zrrd.model.wemedia.dtos.WmChannelDto;
import com.zrrd.model.wemedia.dtos.WmChannelPageReqDto;
import com.zrrd.model.wemedia.pojos.WmChannel;
import com.zrrd.wemedia.mapper.WmChannelMapper;
import com.zrrd.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author WanJl
 * @version 1.0
 * @title WmChannelServiceImpl
 * @description
 * @create 2023/9/21 14:37
 */
@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {
    /**
     * 查询所有频道
     *
     * @return
     */
    @Override
    public ResponseResult findAll() {
        return ResponseResult.okResult(list());
    }

    /**
     * 分页查询频道
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findAll(WmChannelPageReqDto dto) {
        //1.检查参数
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.分页参数检查
        dto.checkParam();
        IPage page=new Page(dto.getPage(),dto.getSize());
        //设置查询条件
        LambdaQueryWrapper<WmChannel> lqw=new LambdaQueryWrapper<>();

        //设置名称模糊查询
        if (StringUtils.isNotBlank(dto.getName())){
            lqw.like(WmChannel::getName,dto.getName());
        }

        lqw.orderByDesc(WmChannel::getCreatedTime);
        //执行带有条件的分页查询，把查询到的信息返回给Page对象
        page=page(page,lqw);

        //返回结果
        ResponseResult responseResult=new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 保存频道
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult save(WmChannelDto dto) {
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmChannel wmChannel = new WmChannel();
        BeanUtils.copyProperties(dto,wmChannel);
        wmChannel.setCreatedTime(new Date());
        wmChannel.setIsDefault(false);
        boolean save = save(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改频道
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult update(WmChannelDto dto) {
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmChannel wmChannel = new WmChannel();
        BeanUtils.copyProperties(dto,wmChannel);
        updateById(wmChannel);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
